// pages/server_end/order/order.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');

Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    index: 2,
    showmoney: false,
    cost: '',
    show: false,
    id:0,
    token:'',
    order:'',
    cost:0,
    orderId:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.setData({
         id:options.id
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          that.getOrder();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getOrder:function(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      id:that.data.id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workerorderdetail, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      //console.log(res.data.data);
      if(res.data.code==1){
         that.setData({
            order:res.data.data,
         })
       }
    })  	
  },
  confirmOrder:function(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      id:that.data.id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workersureorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon:'none'
      })
      if(res.data.code==1){
        setTimeout(function(){
          that.getOrder();
        },1500)
       }
    })  	
  },
  cancelOrder:function(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      id:that.data.id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workercancelorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon:'none'
      })
      if(res.data.code==1){
        setTimeout(function(){
          that.getOrder();
        },1500)
       }
    })  	
  },
  refuseOrder:function(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      id:that.data.id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.refuseorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon:'none'
      })
      if(res.data.code==1){
        setTimeout(function(){
          that.getOrder();
        },1500)
       }
    })  	
  },
  startOrder:function() {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      id:that.data.id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.startorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon:"none"
      })
      if(res.data.code==1){
         that.setData({
            page:1,
         })
         that.getOrder();
       }
    })  	
  },
  inputMoney(){
	  wx.requestSubscribeMessage({
	    tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
    var that = this;
    if(that.data.cost==0){
      wx.showToast({
        title: '请录入大于0的金额',
      })
      return false;
    }
    that.close();
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      id:that.data.id,
      cost:that.data.cost
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.inputorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
	  if(res.data.msg!='')		  
	  {
		  wx.showToast({
		    title: res.data.msg,
		    icon:"none",
        duration: 3000
		  })
	  }
    
      if(res.data.code==1){
        setTimeout(function(){
          that.getOrder();
        },1500)
       }
    })  	
  },
  open() {
	  wx.requestSubscribeMessage({
      tmplIds: [
	  		'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao', //新订单通知
	  		'Cafgw0abMBtNXSJq8BDKhM_cNk0gydfQ845IywRo7KU', //报价更新通知
	  		'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0' //被用户选择通知
	    ],
	    success: (res) => {
	    },
	    fail: (res) => {
	    },
	  })
    console.log('open');
    this.setData({
       show:true,
       showmoney:true
    })
  },
  close() {
    this.setData({
      show:false,
      showmoney:false
   })
  },
  changecost(e){
     var cost =e.detail.value;
     this.setData({
       cost:cost
     })
  },
  chosePosition(e){
    console.log(e);
    var lat =  e.currentTarget.dataset.lat;
    var lng =  e.currentTarget.dataset.lng;
    console.log(lat);
    wx.openLocation({
      latitude:Number(lat),
      longitude:Number(lng)
    })
  },
  makecall(e){
     var phone = e.currentTarget.dataset.phone;
	 console.log(phone);
     wx.makePhoneCall({
       phoneNumber: phone,
     })
  },
  graborder(id){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      id:id
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.graorder, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon:'none'
      })
      if(res.data.code==1){
        setTimeout(function(){
          that.getOrder();
        },1500)
       }
    })  	   
  },
  subStart(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      orderId:id
    })
    wx.requestSubscribeMessage({
        tmplIds: [
          'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao',
          '2cSTpNYwYt9rJ91lfLGnPKS4kc9-qkL_weH6x6UpRXE',
        ],
        success:(res)=> {
          that.graborder(that.data.orderId);							
        },
        fail:(res)=> {
          that.graborder(that.data.orderId);								
        },
      })		
    //that.graborder(that.orderId);	
  },
  goCheckOrder(e) {
    var id = e.currentTarget.dataset.id;
	 var mobile = e.currentTarget.dataset.mobile;
    wx.navigateTo({
      url: '../order_diagnose/order_diagnose?id=' + id+'&phone='+mobile
    })
  },
})